<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
    array('op'=>'list','title'=>'成单明细'),
);
if($_RQ['op']=='list'){
    //$this->model('task')->up_day();
    $return['params']=array();
    $params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
    if(isset($_RQ['mid'])){
        $params['mid']=intval($_RQ['mid']);
        $return['params']['mid']=intval($_RQ['mid']);
    }
    $pindex = max(1, intval($_RQ['page']));
    if($_RQ['psize']){
        $_SESSION['psize']=$_RQ['psize'];
    }
    $psize = $_SESSION['psize']?:10;
    $list=pdo_getslice('jy_guashop_canlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
    if($list){
        $gamepost=array(
            'type'=>1,
            'price'=>198,
        );
        //$this->model('game')->up_addgame($gamepost,5131);
        foreach($list as $k=>&$item){
            $item['member']=cfc('core')->member_show($item['mid']);
            $item['title']=pdo_getcolumn('jy_guashop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'desc');
            $item['daytitle']=$item['dayid'].' '.$this->hourkey[$item['hourkey']];
        }
    }
    $return['list']=$list;
    $totalpage=ceil($total/$psize);
    $totalpage=intval($totalpage);
    $return['totalpage'] =$totalpage;
    $return['params']['page'] =$pindex;
    $htmls=array(
        'type'=>'list',
        'tabs'=>$tabs,
        'export'=>'export',
        'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
            array('name'=>'id','title'=>'记录ID','type'=>'string'),
            array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
            array('name'=>'title','title'=>'挂卖活动','type'=>'string'),
            array('name'=>'fee','title'=>'费用','type'=>'string'),
            //array('name'=>'quannum','title'=>'兑换券','type'=>'string'),
            array('name'=>'daytitle','title'=>'期','type'=>'string'),
            array('name'=>'order_status','title'=>'状态','type'=>'radio','options'=>'order_status'),
            array('name'=>'createtime','title'=>'时间','type'=>'time'),
        ),'action'=>array(
            array('op'=>'address','title'=>'地址_Address','type'=>'modal'),
            array('op'=>'upaddress','title'=>'修改地址','type'=>'modal','show'=>array('order_status'=>1)),
            array('op'=>'fa','title'=>'发货_Ship','type'=>'modal','show'=>array('order_status'=>1)),
            array('op'=>'check3','title'=>'确认完成','type'=>'post','show'=>array('order_status'=>2)),
        )),
        'search'=>array(
            array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
        ),
    );
    $return['order_status']=$this->order_status;
    $return['htmls']=$htmls;
    exi($return);
}
if($_RQ['op']=='address'){
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('canlog',array('id'=>$id),array('id','address','status'));
    if($item['address']){
        $item['address']=json_decode($item['address'],true);
        $item['realname']=$item['address']['name'];
        $item['mobile']=$item['address']['mobile'];
        $item['address']=model('core/member')->get_address_str($item['address']);
    }
    $htmls=array(
        'type'=>'look',
        'desc'=>'收货详细',
    );
    $htmls['look']=array(
        array('type'=>'radio','title'=>'状态','name'=>'order_status','options'=>'order_status'),
        array('type'=>'string','title'=>'联系人','name'=>'realname'),
        array('type'=>'string','title'=>'联系电话','name'=>'mobile'),
        array('type'=>'string','title'=>'收货地址：','name'=>'address'),
    );
    $return['order_status']=$this->order_status;
    $return['htmls']=$htmls;
    $return['item']=$item;
    exi($return);
}
if($_RQ['op']=='check3'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $id=intval($_RQ['id']);
        if(!$id){
            exi('参数错误！','error');
        }
        $this->model('gua')->order_receive($id);
        exi('已确认完成！','','list');
    }
}
if($_RQ['op']=='fa'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $id=intval($post['id']);
        if(!$id){
            exi('参数错误！','error');
        }
        if($post['express_type']==0){
            if(!$post['express'] || !$post['expresssn']){
                exi('请填写快递信息','error');
            }
            $data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
            $this->model('gua')->order_send($id,$data);
        }
        exi('已发货！','','list');
    }
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('canlog',array('id'=>$id));
    $htmls=array(
        'type'=>'edit',
        'desc'=>'订单发货',
    );
    $item['express_type']=0;
    $htmls['edit']=array(
        array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
        array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
    );
    $return['express']=model('jy_weishop/model')->express_types();
    $return['htmls']=$htmls;
    $return['item']=$item;
    exi($return);
}
if($_RQ['op']=='upaddress'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $status=intval($post['status']);
        $id=intval($post['id']);
        $address=array(
            'realname'=>$post['realname'],
            'mobile'=>$post['mobile'],
            'pcd'=>$post['pcd'],
            'address'=>$post['address'],
        );
        pdo_update('jy_guashop_canlog',array('address'=>ijson_encode($address)),array('id'=>$id));
        exi('设置成功！');
    }
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('canlog',array('id'=>$id),array('id','address','order_status'));
    if($item['address']){
        $address=json_decode($item['address'],true);
        $item['address']=$address['address'];
        $item['pcd']=$address['pcd'];
        $item['realname']=$item['address']['name'];
        $item['mobile']=$item['address']['mobile'];
    }
    $htmls=array(
        'type'=>'edit',
        'desc'=>'修改地址信息',
    );
    $htmls['edit']=array(
        array('type'=>'string','title'=>'ID','name'=>'id','read'=>true),
        array('type'=>'string','title'=>'联系人','name'=>'realname'),
        array('type'=>'string','title'=>'联系电话','name'=>'mobile'),
        array('type'=>'pcd','title'=>'收货地址：','name'=>'pcd'),
        array('type'=>'string','title'=>'详细地址：','name'=>'address'),
    );
    $return['order_status']=$this->order_status;
    $return['htmls']=$htmls;
    $return['item']=$item;
    exi($return);
}
if($_RQ['op']=='setstatus'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $status=intval($post['status']);
        $id=intval($post['id']);
        $update=array('status'=>$status);
        pdo_update('jy_guashop_canlog',$update,array('id'=>$id));
        exi('设置成功！');
    }
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('canlog',array('id'=>$id));
    $htmls=array(
        'type'=>'edit',
        'desc'=>'设置状态',
    );
    $htmls['edit']=array(
        array('type'=>'string','title'=>'ID','name'=>'id','read'=>true),
        array('type'=>'string','title'=>'编号','name'=>'logno','read'=>true),
        array('type'=>'radio','title'=>'状态','name'=>'order_status','options'=>'order_status'),
    );
    $return['htmls']=$htmls;
    $return['item']=$item;
    $return['order_status']=$this->order_status;
    exi($return);
}
if($_RQ['op']=='del'){
    $del=trim($_RQ['del'],';');
    if($del){
        $del=explode(';',$del);
    }else{
        $del=array();
    }
    if($_RQ['id']){
        $del[]=$_RQ['id'];
    }
    foreach($del as $id){
        $id=intval($id);
        if($id){
            pdo_delete('jy_guashop_canlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
        }
    }
    exi('删除成功！');
}
if($_RQ['op']=='export'){
    $params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
    if(isset($_RQ['mid'])){
        $params['mid']=intval($_RQ['mid']);
        $return['params']['mid']=intval($_RQ['mid']);
    }
    $list=pdo_getall('jy_guashop_canlog',$params);
    if($list){
        foreach($list as &$item){
            $item['member']=cfc('core')->member_show($item['mid']);
            $item['title']=pdo_getcolumn('jy_guashop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'desc');
            $item['daytitle']=$item['dayid'].' '.$this->hourkey[$item['hourkey']];
            $item['nickname']=$item['member']['nickname'];
            $item['mobile']=$item['member']['mobile'];
            $item['order_status']=$this->order_status[$item['order_status']];
            if($item['address']){
                $item['address']=json_decode($item['address'],true);
                $item['realname']=$item['address']['name'];
                $item['mobile']=$item['address']['mobile'];
                $item['address']=model('core/member')->get_address_str($item['address']);
            }
            $item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
        }
    }
    $header=array(
        'mid'=>'会员ID',
        'nickname'=>'会员昵称',
        'title'=>'挂卖活动',
        'fee'=>'费用',
        'daytitle'=>'期',
        'order_status'=>'状态',
        'realname'=>'联系人',
        'mobile'=>'联系电话',
        'address'=>'收货地址',
        'createtime'=>'时间',
    );
    cfc('tools')->export2excel($header,$list,'成单列表');
}